package com.fg.pro.sys.controller;


import com.fg.pro.model.config.ValidationGroups;
import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.sys.dto.SysUserDto;
import com.fg.pro.model.sys.entity.SysMenu;
import com.fg.pro.model.sys.entity.SysUser;
import com.fg.pro.sys.service.SysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 用户(SysUser)表控制层
 *
 * @author makejava
 * @since 2025-07-17 10:10:47
 */
@Tag(name = "SysUser", description = "用户")
@RestController
@RequestMapping("sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @Operation(summary = "获取用户", description = "userName")
    @GetMapping("/getPage")
    public ResponseResult getPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) String userName) {
        PageInfo pageInfo = sysUserService.getPage(pageNum, pageSize, userName);
        return ResponseResult.okResult(pageInfo);
    }

    @Operation(summary = "获取用户", description = "id")
    @GetMapping("/getById")
    public ResponseResult getById(@RequestParam String id) {
        SysUser sysUser = sysUserService.getUserById(id);
        return ResponseResult.okResult(sysUser);
    }

    @Operation(summary = "添加用户", description = "sysDept")
    @PutMapping("/save")
    public ResponseResult save(@RequestBody(required = true)@Validated(ValidationGroups.Insert.class) SysUser sysUser) {
        boolean save = sysUserService.save(sysUser);
        return ResponseResult.okResult(save ? "添加成功" : "添加失败");
    }

    @Operation(summary = "修改用户", description = "sysDept")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody(required = true)@Validated(ValidationGroups.Update.class) SysUser sysUser) {
        boolean update = sysUserService.updateById(sysUser);
        return ResponseResult.okResult(update ? "修改成功" : "修改失败");
    }

    @Operation(summary = "修改用户状态", description = "sysDept")
    @PostMapping("/updateUserState")
    public ResponseResult updateUserState(@RequestParam(required = true) Long id) {
        SysUser sysUser = sysUserService.getById(id);
        sysUser.setUserState(sysUser.getUserState()==1?0:1);
        boolean update = sysUserService.updateById(sysUser);
        return ResponseResult.okResult(update ? "修改成功" : "修改失败");
    }

    @Operation(summary = "删除用户", description = "ids")
    @DeleteMapping("/del")
    public ResponseResult del(@RequestParam(required = true) List<Long> ids) {
//        List<Long> list = Arrays.asList(ids);
        boolean del = sysUserService.removeBatchByIds(ids);
        return ResponseResult.okResult(del ? "删除成功" : "删除失败");
    }

    @Operation(summary = "获取用户菜单", description = "id")
    @GetMapping ("/getMenuTreeById")
    public ResponseResult getMenuTreeById() {
        List<SysMenu> list = sysUserService.getMenuTreeById();
        return ResponseResult.okResult(list);
    }
    @Operation(summary = "管理端登入", description = "参数为账号、密码")
    @GetMapping ("/login")
    public ResponseResult login(@RequestParam(required = true) String userAccount,
                                       @RequestParam(required = true) String userPassword) {
        SysUserDto sysUserDto = sysUserService.login(userAccount,userPassword);
        return ResponseResult.okResult(sysUserDto);
    }

    //    ----------------------------------远程服务------------------------------
    @Operation(summary = "获取用户", description = "id")
    @GetMapping("/feignGetById")
    public SysUserDto feignGetById(@RequestParam String id) {
        SysUserDto sysUserDto = sysUserService.getSysUserDtoById(id);
        return sysUserDto;
    }
}

